<template>
  <div class="content">
    <el-card class="card-box">
      <div class="card-box--headerwrap">
        <el-page-header content="详情" @back="goBack"></el-page-header>
        <!-- <div v-if="!isChange && applyStatus != '3' && isApproval">
          <el-button type="primary" @click="approve">通过</el-button>
          <el-button type="primary" @click="reject">驳回</el-button>
        </div>

        <div v-else>
          <template v-if="isApproval">
            <template v-if="!model.isNoReject">
              <el-button
                type="primary"
                :loading="model.saveLoading"
                @click="handleReject"
                >驳回</el-button
              >
            </template>
            <el-button
              type="primary"
              :loading="model.saveLoading"
              @click="handleSubmit"
              >通过</el-button
            >
          </template>
        </div> -->
      </div>
    </el-card>

    <el-card class="card-box form-box change-form" v-if="!isChange">
      <template #header>终止申请信息 </template>
      <div class="card-box-content">
        <el-form label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目名称">
                {{
                  model.changeFormData.projectNameOld ||
                  model.changeFormData.projectName
                }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目编号">
                {{ model.changeFormData.projectCode }}
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="终止原因">
                {{ model.changeFormData.alterationReason }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请人">
                {{ model.changeFormData.createUserName }}
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="终止事由">
                {{ model.changeFormData.alterationReason }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请单位">
                {{ model.changeFormData.ext2 }}
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="终止时间">
                {{ model.changeFormData.alterationTime }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请时间">
                {{ model.changeFormData.createTime }}
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col>
              <el-form-item label="备注">
                {{ model.changeFormData.remark }}
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-card>

    <el-card class="card-box form-box change-form" v-else>
      <template #header> 变更申请信息 </template>
      <div class="card-box-content">
        <el-form label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目名称">
                {{
                  model.changeFormData.projectNameOld ||
                  model.changeFormData.projectName
                }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目编号">
                {{ model.changeFormData.projectCode }}
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="变更原因">
                {{ model.changeFormData.alterationMatters }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请人">
                {{ model.changeFormData.createUserName }}
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="变更事由">
                {{ model.changeFormData.alterationReason }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请单位">
                {{ model.changeFormData.ext2 }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="变更时间">
                {{ model.changeFormData.alterationTime }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请时间">
                {{ model.changeFormData.createTime }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="备注">
                {{ model.changeFormData.remark }}
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-card>

    <el-card class="card-box form-box" v-if="baseInfo != null">
      <template #header> 基础信息相关变更 </template>
      <div v-html="baseInfo" class="card-box-content"></div>
    </el-card>
    <el-card
      class="card-box form-box"
      v-if="model.projectTargetList.length > 0"
    >
      <template #header>项目目标及考核指标相关变更 </template>
      <Goal :data="model.projectTargetList" :disabled="true" />
      <!-- <div v-html="baseInfo" class="card-box-content"></div> -->
    </el-card>

    <el-card
      class="card-box form-box"
      v-if="model.projectPlan && model.projectPlan.length"
    >
      <template #header>项目计划相关变更 </template>
      <Plan :data="model.projectPlan" :disabled="true" />
      <!-- <div v-html="baseInfo" class="card-box-content"></div> -->
    </el-card>

    <br />
    <el-card v-if="route.query.instanceId">
      <template #header>
        <div class="card-box-header">
          <span> 流程状态</span>
        </div>
      </template>
      <div>
        <el-collapse-transition v-if="currentStep">
          <!-- <status-info
            :currentStep="currentStep"
            :steps="steps"
            :isSkip="isSkipAlter"
          /> -->
          <img :src="currentStep" />
        </el-collapse-transition>
      </div>
    </el-card>
    <br />
    <el-card v-if="route.query.instanceId">
      <template #header>
        <div class="card-box-header">
          <span> 流转记录</span>
        </div>
      </template>
      <div>
        <el-collapse-transition>
          <approval-info :activities="approveRecord" />
        </el-collapse-transition>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import Goal from "../../create/components/goal.vue";
import Plan from "../../create/components/plan.vue";
import { ref, onMounted, reactive } from "vue";
import { useRouter, useRoute } from "vue-router";
import StatusInfo from "/@/components/StatusInfo/index.vue";
import ApprovalInfo from "/@/components/ApprovalInfo/index.vue";
import { flowTypeObj } from "/@/components/approveDialog/type";
import { flowStatusObj } from "/@/components/StatusInfo/type";
import { postAlterationInfo } from "/@/api/change";
import { getApproveHistory, getApproveImg } from "/@/api/project";

const router = useRouter();
const route = useRoute();
const nodeId = route.query.nodeId ? route.query.nodeId : "ACT_010";
const isSkipAlter = route.query.isSkipAlter;
const currentStep = ref(null);
const steps = ref([]);
const approveRecord = ref([]);
let baseInfo = ref(null);
let contractInfo = ref(null);
const model = reactive({
  reason: "",
  flowType: "2001",
  approveHandlerId: null,
  isNoReject: false,
  dialogType: "approval",
  saveLoading: false,
  projectTargetList: [],
  projectPlan: [],
  changeFormData: {
    projectName: "",
    projectNameOld: "",
    projectCode: "",
    createTime: "",
    createUserName: "",
    ext2: "",
    alterationMatters: "",
    alterationReason: "",
    alterationTime: "",
    remark: "",
    applyStatus: "1"
  },
  changeRecordFileList: [],
  expertRecordFileList: [],
  otherFileList: []
});

const goBack = (): void => {
  router.push("/project/change");
};

const getDetail = async (id: string): Promise<any> => {
  try {
    const { code, data } = await postAlterationInfo({ id });
    if (code === 200) {
      if (data.alterJsonInfo) {
        const {
          alterBaseInfo,
          alterContractInfo,
          alterTargetInfo,
          alterPlanInfo
        } = JSON.parse(data.alterJsonInfo);

        baseInfo.value = alterBaseInfo || null;
        contractInfo.value = alterContractInfo;
        model.projectTargetList = alterTargetInfo || [];

        model.projectPlan =
          alterPlanInfo &&
          alterPlanInfo.map(el => {
            return {
              time:
                el.startTime.split(" ")[0] + "至" + el.endTime.split(" ")[0],
              timeObj: [el.startTime.split(" ")[0], el.endTime.split(" ")[0]],
              content: el.researchContent,
              goal: el.goal,
              alterationType: el.alterationType
            };
          });
      }
      model.changeFormData = data;

      model.flowType = data.approvalTypeCode;
      if (model.flowType) {
        model.isNoReject = flowTypeObj[model.flowType][nodeId].isNoReject; //不展示驳回按钮(包括驳回上一级和回退到指定节点)
        getStep();
      }
      if (data.alterationSignRecord) {
        model.changeRecordFileList = JSON.parse(data.alterationSignRecord);
        model.changeRecordFileList.map(el => {
          el["name"] = el.fileName;
        });
      }
      if (data.expertSignRecord) {
        model.expertRecordFileList = JSON.parse(data.expertSignRecord);
        model.expertRecordFileList.map(el => {
          el["name"] = el.fileName;
        });
      }
      if (data.otherFile) {
        model.otherFileList = JSON.parse(data.otherFile);
        model.otherFileList.map(el => {
          el["name"] = el.fileName;
        });
      }
    }
  } catch (err) {
    console.log(err);
  }
};
//查询流程审批记录
const getApproveHistoryList = async instanceId => {
  const { code, data } = await getApproveHistory(instanceId);
  if (code === 200) {
    approveRecord.value = data;
  }
};

//获取流程转态
const getStep = async () => {
  const instanceId = route.query.instanceId;
    const { code, data } = await getApproveImg(instanceId);
    if (code === 200) {
      currentStep.value = data;
    }
};

const isChange =
  route.query.mode === "change" || route.query.mode === "approval"
    ? true
    : false;
onMounted(() => {
  if (route.query.id) {
    getDetail(route.query.id as string);
  }
  if (route.query.instanceId) {
    getApproveHistoryList(route.query.instanceId);
  }
});
</script>

<style lang="scss" scoped>
.content {
  :deep .el-page-header__content {
    font-size: 16px;
  }
  .card-box {
    margin-bottom: 10px;
    &--headerwrap {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    &--setbtn {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      width: 140px;
    }
    &-content {
      color: #606266;
      font-size: 14px;
    }
  }
  :deep .change-form {
    .el-form-item__content {
      display: flex;
      align-items: center;
      .record {
        display: flex;
        align-items: center;
        .el-upload-list__item:first-child {
          margin-top: 0;
        }
      }
    }
  }

  .header__wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}

.width-custom220 {
  width: 220px;
}
.form-box {
  margin-top: 10px;
}
.form-info {
  margin-bottom: 20px;
}
.header-btns {
  text-align: right;
}

// .el-select-dropdown__item {
//   height: auto;
//   // min-height: 100px;
//   // max-height: 274px;
//   padding: 0;
//   overflow-x: hidden;
//   overflow-y: hidden;
// }
// .el-select-dropdown_item.selected {
//   font-weight: normal;
// }

// .el-select-dropdown_item.selected {
//   font-weight: normal;
// }

// // .tree-option
// .tree-option :deep .el-tree-node__content {
//   height: auto;
//   padding: 0 10px;
// }
// // .tree-option :deep .el-select-dropdown__list {
// //   padding: 0 6px;
// // }
// .tree-option :deep .el-tree-node__label {
//   font-weight: normal;
//   padding: 0 10px;
//   margin-right: 10px;
// }

// .el-tree :deep .is-current .el-tree-node__label {
//   color: #409eff;
//   font-weight: 700;
// }
// .el-tree :deep .is-current .el-tree-node__children .el-tree-node__label {
//   color: #606266;
//   font-weight: normal;
// }
// .default-option :deep .el-tree-node__label {
//   font-weight: normal;
//   padding: 0 10px;
//   // margin-right: 10px;
// }

// .default-option {
//   padding: 0 10px;
// }
</style>
